package com.lineage.server.model;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_PinkName;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.timecontroller.server.ServerWarExecutor;
import com.lineage.server.types.Point;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1PinkName {
  public static final Log _log = LogFactory.getLog(L1PinkName.class);
  
  public static void onAction(L1PcInstance tgpc, L1Character atk) {
    if (tgpc == null || atk == null)
      return; 
    if (!(atk instanceof L1PcInstance))
      return; 
    L1PcInstance attacker = (L1PcInstance)atk;
    if (tgpc.getId() == attacker.getId())
      return; 
    if (attacker.getFightId() == tgpc.getId())
      return; 
    if (tgpc.getZoneType() != 0)
      return; 
    if (attacker.getZoneType() != 0)
      return; 
    boolean isNowWar = false;
    int castleId = L1CastleLocation.getCastleIdByArea((L1Character)tgpc);
    if (castleId != 0)
      isNowWar = ServerWarExecutor.get().isNowWar(castleId); 
    if (isNowWar)
      return; 
    if (tgpc.getLawful() < 0)
      return; 
    if (tgpc.isPinkName())
      return; 
    attacker.sendPacketsAll((ServerBasePacket)new S_PinkName(attacker.getId(), 15));
    if (!attacker.isPinkName()) {
      attacker.setPinkName(true);
      attacker.setSkillEffect(5122, 15000);
      PinkNameTimer pink = new PinkNameTimer(attacker);
      GeneralThreadPool.get().execute(pink);
    } 
  }
  
  public static void runPinkNameTimer(L1PcInstance pc) {
    if (!pc.isPinkName()) {
      pc.sendPacketsAll((ServerBasePacket)new S_PinkName(pc.getId(), 15));
      pc.setPinkName(true);
      pc.setSkillEffect(5122, 15000);
      PinkNameTimer pink = new PinkNameTimer(pc);
      GeneralThreadPool.get().execute(pink);
    } 
  }
  
  private static class PinkNameTimer implements Runnable {
    private L1PcInstance _attacker;
    
    private Point _point;
    
    private int _mapid;
    
    public PinkNameTimer(L1PcInstance attacker) {
      this._attacker = null;
      this._point = null;
      this._mapid = -1;
      this._attacker = attacker;
      this._point = new Point(attacker.getX(), attacker.getY());
      this._mapid = attacker.getMapId();
    }
    
    public void run() {
      int i = 0;
      while (i < 180) {
        try {
          Thread.sleep(1000L);
        } catch (Exception e) {
          L1PinkName._log.error(e.getLocalizedMessage(), e);
        } 
        if (this._mapid != this._attacker.getMapId())
          break; 
        if (!this._attacker.getLocation().isInScreen(this._point))
          break; 
        if (this._attacker.isDead())
          break; 
        i++;
      } 
      stopPinkName();
    }
    
    private void stopPinkName() {
      this._attacker.sendPacketsAll((ServerBasePacket)new S_PinkName(this._attacker.getId(), 0));
      this._attacker.setPinkName(false);
    }
  }
}
